﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace eShop.Services
{
    public static class MvcHtmlHelper
    {
        /// <summary>
        /// 获取属性配件名称
        /// </summary>
        /// <param name="html"></param>
        /// <param name="selectedValue">以 ','分割的属性值</param>
        /// <returns></returns>
        public static MvcHtmlString GetAttributes(this HtmlHelper html, string selectedValue)
        {
            if (selectedValue.Trim() == "undefined") return MvcHtmlString.Create("无");
            if (selectedValue.Trim() == "") return MvcHtmlString.Create("无");
            string[] arrTemp = selectedValue.Split('_');
            string attributeValueInfo = "";
            foreach (var item in arrTemp)
            {
                if (string.IsNullOrWhiteSpace(item.Trim())) continue;

                IDataReader reader = eShop.Core.BSPData.RDBS.GetAttributeValueById(int.Parse(item));
                if (reader.Read())
                {
                    attributeValueInfo += reader["attrvalue"].ToString() + ",";
                }
            }
            return MvcHtmlString.Create(attributeValueInfo.Substring(0, attributeValueInfo.Length - 1));
        }


        /// <summary>
        /// 获取属性配件值价格总和
        /// </summary>
        /// <param name="selectedValue">属性值</param>
        /// <param name="num">数量</param>
        /// <returns></returns>
        public static decimal GetAttributesTotalPrice(string selectedValue, int num = 1)
        {
            if (selectedValue.Trim() == "undefined") return 0;
            string[] arrTemp = selectedValue.Split('_');
            decimal sum = 0M;
            foreach (var item in arrTemp)
            {
                if (string.IsNullOrWhiteSpace(item.Trim())) continue;

                IDataReader reader = eShop.Core.BSPData.RDBS.GetAttributeValueById(int.Parse(item));
                if (reader.Read())
                {
                    var temp = reader["attrprice"].ToString();
                    if (string.IsNullOrWhiteSpace(temp)) temp = "0";
                    sum += decimal.Parse(temp) * num;
                }
            }
            return sum;

        }

    }
}
